

#import "FAMMineHeightViewController.h"

@interface FAMMineHeightViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UIButton *FAMFtButton;
@property (weak, nonatomic) IBOutlet UIButton *FAMCmButton;
@property (weak, nonatomic) IBOutlet UIPickerView *FAMFtPickerView;
@property (nonatomic, strong)NSMutableArray *FAMFtArray;
@property (nonatomic, strong)NSMutableArray *FAMInArray;
@property (nonatomic, strong)NSMutableArray *FAMCmArray;
@property (nonatomic, strong)NSString *FAMHeight;
@property (nonatomic, assign)NSInteger FAMFt,FAMIn;
@end

@implementation FAMMineHeightViewController
- (NSMutableArray *)FAMFtArray{
    if (!_FAMFtArray) {
        _FAMFtArray = [NSMutableArray array];
        for (int i = 2; i <= 8; i++) {
            [_FAMFtArray addObject:[NSString stringWithFormat:@"%d",i]];
        }
    }
    return _FAMFtArray;
}
- (NSMutableArray *)FAMInArray{
    if (!_FAMInArray) {
        _FAMInArray = [NSMutableArray array];
        for (int i = 0; i<= 9; i++) {
            [_FAMInArray addObject:[NSString stringWithFormat:@"%d",i]];
        }
    }
    return _FAMInArray;
}
- (NSMutableArray *)FAMCmArray{
    if (!_FAMCmArray) {
        _FAMCmArray = [NSMutableArray array];
        for (int i = 50; i<= 250; i++) {
            [_FAMCmArray addObject:[NSString stringWithFormat:@"%d",i]];
        }
    }
    return _FAMCmArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self FAM_showUi];
}
- (void)FAM_showUi{
    self.FAMFtPickerView.delegate = self;
    self.FAMFtPickerView.dataSource = self;
    self.FAMHeight = [FAMUserManger shareInstance].userModel.height;
    if (self.FAMFtButton.isSelected) {
        [self.FAMFtPickerView selectRow:4 inComponent:0 animated:YES];
        [self.FAMFtPickerView selectRow:5 inComponent:1 animated:YES];
    }else{
        [self.FAMFtPickerView selectRow:115 inComponent:0 animated:YES];
    }
    
}
- (IBAction)FAM_ftButtonClick:(UIButton *)sender {
    [sender setSelected:YES];
    sender.backgroundColor = kBlackColor;
    [sender setTitleColor:kWhiteColor forState:UIControlStateNormal];
    self.FAMCmButton.backgroundColor = kClearColor;
    [self.FAMCmButton setTitleColor:kBlackColor forState:UIControlStateNormal];
    [self.FAMCmButton setSelected:NO];
    [self.FAMFtPickerView reloadAllComponents];
    [self.FAMFtPickerView selectRow:4 inComponent:0 animated:YES];
    [self.FAMFtPickerView selectRow:5 inComponent:1 animated:YES];
    self.FAMFt = ((NSString *)self.FAMFtArray[4]).integerValue;
    self.FAMIn = ((NSString *)self.FAMInArray[5]).integerValue;
    self.FAMHeight = [NSString stringWithFormat:@"%0.2f",self.FAMFt * ft + self.FAMIn * ins];
}
- (IBAction)FAM_cmButtonClick:(UIButton *)sender {
    [sender setSelected:YES];
    sender.backgroundColor = kBlackColor;
    [sender setTitleColor:kWhiteColor forState:UIControlStateNormal];
    self.FAMFtButton.backgroundColor = kClearColor;
    [self.FAMFtButton setTitleColor:kBlackColor forState:UIControlStateNormal];
    [self.FAMFtButton setSelected:NO];
    [self.FAMFtPickerView reloadAllComponents];
    [self.FAMFtPickerView selectRow:114 inComponent:0 animated:YES];
    self.FAMHeight = self.FAMCmArray[114];
}
#pragma mark ---- UIPickerViewDelegate UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    if (self.FAMFtButton.isSelected) {
        return 2;
    }else{
        return 1;
    }
    
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (self.FAMFtButton.isSelected) {
        if (component == 0) {
            return self.FAMFtArray.count;
        }else{
            return self.FAMInArray.count;
        }
    }else{
        return self.FAMCmArray.count;
    }
    
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (self.FAMFtButton.isSelected) {
        if (component == 0) {
            return [NSString stringWithFormat:@"%@ft",self.FAMFtArray[row]];
        }else{
            return [NSString stringWithFormat:@"%@in",self.FAMInArray[row]];
        }
    }else{
        return [NSString stringWithFormat:@"%@cm",self.FAMCmArray[row]];
    }
    
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (self.FAMFtButton.isSelected) {
        if (component == 0) {
            self.FAMFt = ((NSString *)self.FAMFtArray[row]).integerValue;
        }else{
            self.FAMIn = ((NSString *)self.FAMInArray[row]).integerValue;
        }
        self.FAMHeight = [NSString stringWithFormat:@"%0.2f",self.FAMFt * ft + self.FAMIn * ins];
    }else{
        self.FAMHeight = self.FAMCmArray[row];
    }
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 60;
}
- (IBAction)FAMSaveButtonClick:(UIButton *)sender {
    WeakSelf(self);
    [self showHudInKeyWindowWithHint:nil];
    [FAMHttpTool FAM_PutFoodmateUserdataWithParameters:[NSMutableDictionary dictionaryWithDictionary:@{@"data":@{@"height":self.FAMHeight}}] success:^(FAMFoodmateUserdataModel * _Nonnull model, NSString * _Nonnull msg) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf hideHudKeyWindow];
            [FAMUserManger shareInstance].userModel.height = weakSelf.FAMHeight;
            [weakSelf.navigationController popViewControllerAnimated:YES];
        });
    } failure:^(NSString * _Nonnull errorInfo) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf hideHudKeyWindow];
            [weakSelf showHint:errorInfo];
        });
    }];
}

@end
